package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.OrderDao;
import com.qf.xiaomi.pojo.Order;
import com.qf.xiaomi.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public void insert(Order order) {
        QueryRunner qr = new QueryRunner();

        String sql = "INSERT INTO `tb_order` (`id`, `uid`, `money`, `status`, `time`, `aid`) VALUES (?,?,?,?,?,?)";

        try {
            qr.update(DataSourceUtils.getConnection(), sql, order.getId(), order.getUid(),order.getMoney(), order.getStatus(),order.getTime(), order.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void updateStatus(String orderId, String status) {
        QueryRunner qr = new QueryRunner();

        String sql = "update tb_order set status = ? where id = ?";

        try {
            qr.update(DataSourceUtils.getConnection(), sql, status, orderId );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Order> getOrderList(int uid) {
        QueryRunner qr = new QueryRunner();

        String sql = "select * from tb_order where uid = ?";

        try {
            List<Order> orderList = qr.query(DataSourceUtils.getConnection(), sql, new BeanListHandler<>(Order.class), uid);
            return orderList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
